#include "OpenFile.h"
#include "ui_OpenFile.h"

#include <QFileDialog>
#include <QDebug>

OpenFile::OpenFile(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OpenFile)
{
    ui->setupUi(this);

    connect(ui->selectFileBtn,&QPushButton::clicked,this,&OpenFile::onSelect);
    connect(ui->okBtn,&QPushButton::clicked,this,&OpenFile::onOpen);
    connect(ui->cancelBtn,&QPushButton::clicked,this,&OpenFile::onCancel);

    setWindowModality(Qt::ApplicationModal);
}

OpenFile::~OpenFile()
{
    delete ui;
}

void OpenFile::onSelect()
{
    QString fileName = QFileDialog::getOpenFileName(
                this, QObject::tr("Open File"),
                QDir::homePath(),
                QString("ALL Files (*.*)"));

    qDebug() << fileName;

    ui->fileNameEdit->setText(fileName);
}

void OpenFile::onOpen()
{
    if (ui->passEdit->text().isEmpty() || ui->fileNameEdit->text().isEmpty())
        return;
    emit openDBFile(ui->fileNameEdit->text(),ui->passEdit->text());
    this->close();
}

void OpenFile::onCancel()
{
    this->close();
}
